﻿Public Class VACCINE
    Dim connect As CONDB = CONDB.NewConnection
    Dim s As FILTERCLASS
    Dim sql As String
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Private Sub VACCINE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridVaccine()
        SearchMTRGTIDTxt()
        MASVACCINEICDGrid.PrimaryGrid.Rows.Add(New DevComponents.DotNetBar.SuperGrid.GridRow({"", "", "", ""}))
        showdatagrid()
        MASVACCINEGrid.PrimaryGrid.SetSort(MASVACCINEGrid.PrimaryGrid.Columns(6), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)

        DesignForm(Me)
        DesignGrid(MASVACCINEGrid)
        DesignGrid(MASVACCINEICDGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub
    Private Sub SelectDatagridVaccine()
        Dim objSelectDataVaccine As New VACCINECLASS
        Dim Vaccine As DataTable
        Vaccine = connect.GetTable(objSelectDataVaccine.SelectVaccine_)
        MASVACCINEGrid.PrimaryGrid.DataSource = Vaccine
    End Sub
    Private Sub SearchMTRGTIDTxt()
        Dim sql2 As String
        sql2 = "SELECT CAST(drgcode AS CHAR(10)) AS drgcode,genericname,prdcode FROM drugitem WHERE status = 1 "
        s = New FILTERCLASS(PRDCODETxt, sql2, "รหัสยา,ชื่อสามัญทางยา,prdcode", "120,288,150", "1,1,0", "1,1,0")
        s.SetTagIndex = 2 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub

    Private Sub cleardata()
        VACCINETxt.Text = ""
        VACCODETxt.Text = ""
        VACCINE_ENTxt.Text = ""
        VACCINE_THTxt.Text = ""
        PRDCODETxt.Text = ""
        AGE_MTxt.Text = ""
        REMARKTxt.Text = ""
        MasVaccinestatuscheck.Checked = False
        F_VIP.Checked = False
    End Sub

    Private Sub AGE_MTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub MASVACCINEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASVACCINEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'MsgBox(CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("billid").Value())
        If grid.Cells("vaccineid").Value() IsNot DBNull.Value Then
            VACCINETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("vaccineid").Value()
        Else
            VACCINETxt.Text = ""
        End If
        If grid.Cells("รหัสวัคซีน").Value() IsNot DBNull.Value Then
            VACCODETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสวัคซีน").Value()
        Else
            VACCODETxt.Text = ""
        End If
        If grid.Cells("ชื่อสากล").Value() IsNot DBNull.Value Then
            VACCINE_ENTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสากล").Value()
        Else
            VACCINE_ENTxt.Text = ""
        End If
        If grid.Cells("ชื่อไทย").Value() IsNot DBNull.Value Then
            VACCINE_THTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อไทย").Value()
            vacnameLa.Text = "รายการโรคที่ " & CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อไทย").Value() & " ครอบคลุม"
            vacnameLa.Visible = True
        Else
            VACCINE_THTxt.Text = ""
            vacnameLa.Text = ""
            vacnameLa.Visible = False
        End If
        If grid.Cells("prdcode").Value() IsNot DBNull.Value Then
            PRDCODETxt.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prdcode").Value()
        Else
            PRDCODETxt.Tag = ""
        End If
        If grid.Cells("รหัสยา").Value() IsNot DBNull.Value Then
            PRDCODETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสยา").Value()
        Else
            PRDCODETxt.Text = ""
        End If
        If grid.Cells("อายุเดือน").Value() IsNot DBNull.Value Then
            AGE_MTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อายุเดือน").Value()
        Else
            AGE_MTxt.Text = ""
        End If
        If grid.Cells("หมายเหตุ").Value() IsNot DBNull.Value Then
            REMARKTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value()
        Else
            REMARKTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            MasVaccinestatuscheck.Checked = True
        Else
            MasVaccinestatuscheck.Checked = False
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("f_vip").Value() = 1 Then
            F_VIP.Checked = True
        Else
            F_VIP.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masvaccine SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `vaccineid` = '" & VACCINETxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If

        Dim dt As New DataTable
        sql = "SELECT masdiagcode.`ICDDESC_TH` as 'ชื่อรหัสโรคภาษาไทย',masdiagcode.`icdid`,`vicdid` FROM masvaccineicd LEFT JOIN (SELECT `icdid`,`icddesc_th` FROM masdiagcode where `status` = 1) as masdiagcode on masvaccineicd.`icdid` = masdiagcode.`icdid` WHERE masvaccineicd.`vaccineid` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("vaccineid").Value & "';"
        dt = connect.GetTable(sql)
        MASVACCINEICDGrid.PrimaryGrid.Rows.Clear()
        For i As Integer = 0 To dt.Rows.Count - 1
            MASVACCINEICDGrid.PrimaryGrid.Rows.Add(New DevComponents.DotNetBar.SuperGrid.GridRow({dt.Rows(i)("ชื่อรหัสโรคภาษาไทย").ToString, dt.Rows(i)("icdid").ToString, dt.Rows(i)("vicdid").ToString}))
        Next
        MASVACCINEICDGrid.PrimaryGrid.Rows.Add(New DevComponents.DotNetBar.SuperGrid.GridRow({"", DBNull.Value, DBNull.Value}))

    End Sub

 
    Private Sub showdatagrid()
        sql = "SELECT icdcode,icddesc_th,icdid FROM masdiagcode WHERE `status` = 1 ;"
        Dim zx As New FILTERCLASSGRID(MASVACCINEICDGrid, sql, "รหัส ICD,รหัสโรคภาษาไทย,รหัส", "50,150,50", "1,0,0", "1,0,0")
        zx.SetShowBorder = True
        zx.SetShowInColumnName = "ชื่อรหัสโรคภาษาไทย"
        zx.SetTagIndex = 2
        zx.SetTextIndex = 1
        zx.SetShowInDown = True
        'zx.SetShowInColumnName = "ชื่อไทย"
    End Sub
    Private Sub MASVACCINEICDGrid_CellValueChanged(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellValueChangedEventArgs) Handles MASVACCINEICDGrid.CellValueChanged
        If e.GridCell.GridColumn.Name = "ชื่อรหัสโรคภาษาไทย" Then
            If CType(e.GridPanel.Rows(MASVACCINEICDGrid.PrimaryGrid.Rows.Count - 1), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อรหัสโรคภาษาไทย").Value.ToString.Trim <> "" Then
                MASVACCINEICDGrid.PrimaryGrid.Rows.Add(New DevComponents.DotNetBar.SuperGrid.GridRow({"", DBNull.Value, DBNull.Value}))
            End If
            CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("icdid").Value = Convert.ToString(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อรหัสโรคภาษาไทย").Tag)
        End If
        Dim sql As String
        If connect.GetTable("SELECT `icdid` FROM masvaccineicd ;").Rows.Count > 0 Then
            sql = "UPDATE masvaccineicd SET `icdid` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("icdid").Value & "' "
            sql += "WHERE `vicdid` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("vicdid").Value & "' "
            connect.ExecuteNonQuery(sql)
        End If
    End Sub


    Private Sub save_Click_1(sender As Object, e As EventArgs) Handles save.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If VACCODETxt.Text.Length.ToString > 0 And VACCINE_THTxt.Text.Length.ToString > 0 And VACCINE_ENTxt.Text.Length.ToString > 0 Then

                    Dim objinsertdata As New VACCINECLASS
                    objinsertdata.VACCODE_ = VACCODETxt.Text
                    objinsertdata.VACCINE_EN_ = VACCINE_ENTxt.Text
                    objinsertdata.VACCINE_TH_ = VACCINE_THTxt.Text
                    If Convert.ToString(PRDCODETxt.Tag).Trim <> "" Then
                        objinsertdata.PRDCODE_ = Convert.ToString(PRDCODETxt.Tag).Trim
                    End If

                    If AGE_MTxt.Text = "" Then
                        objinsertdata.AGE_M_ = Nothing
                    Else
                        objinsertdata.AGE_M_ = CInt(AGE_MTxt.Text)
                    End If

                    'objinsertdata.STATUS_ = MasVaccinestatuscheck.Checked
                    objinsertdata.REMARK_ = REMARKTxt.Text
                    objinsertdata.F_VIP_ = F_VIP.Checked

                    If objinsertdata.insertVaccineData(MASVACCINEICDGrid, PRDCODETxt.Text.Length, AGE_MTxt.Text.Length) = True Then
                        MsgBox("ท่านได้ทำการเพิ่มข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
                        SelectDatagridVaccine()
                        cleardata()
                    End If

                    'Else
                    'For i As Integer = 0 To MASVACCINEICDGrid.PrimaryGrid.Rows.Count - 1
                    '    If CType(MASVACCINEICDGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("icdid").Value.ToString.Trim <> "" Then
                    '        'sql = "SELECT `icdid` FROM masvaccineicd WHERE `vicdid` = '" & CType(MASVACCINEICDGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("vicdid").Value & "'"
                    '        'If connect.GetTable(sql).Rows.Count = 0 Then
                    '        If CType(MASVACCINEICDGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("vicdid").Value.ToString.Trim = "" Then
                    '            sql = ""
                    '            sql += "INSERT INTO masvaccineicd "
                    '            sql += "(`vaccineid`,`icdid`)"
                    '            sql += "VALUES ("
                    '            sql += "'" & VACCINETxt.Text & "',"
                    '            sql += "'" & CType(MASVACCINEICDGrid.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("icdid").Value & "');"
                    '        End If

                    '    End If
                    'Next i
                    'If sql <> "" Then
                    '    connect.ExecuteNonQuery(sql)

                    '    SelectDatagridVaccine()

                    '    cleardata()

                    '    MASVACCINEGrid.PrimaryGrid.SetSort(MASVACCINEGrid.PrimaryGrid.Columns(6), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)
                    'End If
                    'MsgBox("ท่านได้ทำการเพิ่มข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)

                    'SelectDatagridVaccine()

                    'cleardata()

                    'MASVACCINEGrid.PrimaryGrid.SetSort(MASVACCINEGrid.PrimaryGrid.Columns(6), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)
                Else
                    MsgBox("โปรดกรุณาระบุข้อมูลชื่อวัคซีนและรหัสวัคซีนให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub edit_Click_1(sender As Object, e As EventArgs) Handles edit.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If VACCODETxt.Text.Length.ToString > 0 And VACCINE_THTxt.Text.Length.ToString > 0 And VACCINE_ENTxt.Text.Length.ToString > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New VACCINECLASS
                        objeditData.VACCINEID_ = VACCINETxt.Text
                        objeditData.VACCODE_ = VACCODETxt.Text
                        objeditData.VACCINE_EN_ = VACCINE_ENTxt.Text
                        objeditData.VACCINE_TH_ = VACCINE_THTxt.Text
                        If Convert.ToString(PRDCODETxt.Tag) <> "" Then
                            objeditData.PRDCODE_ = Convert.ToString(PRDCODETxt.Tag)
                        End If

                        If AGE_MTxt.Text = "" Then
                            objeditData.AGE_M_ = Nothing
                        Else
                            objeditData.AGE_M_ = CInt(AGE_MTxt.Text)
                        End If

                        objeditData.REMARK_ = REMARKTxt.Text
                        'objeditData.STATUS_ = MasVaccinestatuscheck.Checked
                        objeditData.F_VIP_ = F_VIP.Checked
                        If objeditData.editVaccinedata(MASVACCINEICDGrid, PRDCODETxt.Text.Length, AGE_MTxt.Text.Length) = True Then
                            MsgBox("ท่านได้ทำการแก้ไขข้อมูลวัคซีนเรียบร้อยแล้ว", MsgBoxStyle.Information)
                            SelectDatagridVaccine()
                            cleardata()
                        End If

                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("vacduration")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("vacduration")
        nextform.ShowDialog()
    End Sub
End Class